package com.luo.controller;

import com.luo.config.Log;
import com.luo.pojo.Article;
import com.luo.pojo.CommentsInfo;
import com.luo.pojo.User;
import com.luo.service.comment.CommentService;
import com.luo.service.content.ContentService;
import com.luo.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController

public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    @Autowired
    private ContentService contentService;

    @GetMapping("/getcomments/{ownerId}")
    public List<CommentsInfo> getOwnerIdComment(@PathVariable String ownerId){
        List<CommentsInfo> ownersAllComments = this.commentService.getOwnersAllComments(ownerId);
        return ownersAllComments;
    }

    @Log(logStr = "用户删除一条评论")
    @PostMapping("/deleteComments/{ownerId}")
    public List<CommentsInfo> deleteOnFather(HttpServletRequest request, @PathVariable String ownerId){
        System.out.println(request.getHeader("id"));
        System.out.println(request.getHeader("commentId"));
        if(request.getHeader("commentId").equals("null")){
            this.commentService.isDeletedF(Integer.parseInt(request.getHeader("id")),request.getHeader("createTime"));
        }else if(request.getHeader("id").equals("null")){
            this.commentService.isDeletedR(Integer.parseInt(request.getHeader("commentId")),request.getHeader("createTime"));
        }
        return this.commentService.getOwnersAllComments(ownerId);
    }

    @Log(logStr = "用户新增一条评论")
    @PostMapping("/insertFatherComments/{ownerId}")
    public List<CommentsInfo> addFatherComment( @PathVariable String ownerId,@RequestBody HashMap<String,String> data){
        String userName = data.get("from_name");
        User user = this.userService.getUserByName(userName);
        this.commentService.isInsertedF(Integer.parseInt(data.get("type")),ownerId,user.getUid(),userName,user.getAvatar(),data.get("content"));
        return this.commentService.getOwnersAllComments(ownerId);
    }

    @Log(logStr = "用户新增一条回复")
    @PostMapping("/insertReplyComments/{ownerId}")
    public List<CommentsInfo> addReplyComment(@PathVariable String ownerId,@RequestBody HashMap<String,String> data){
        String fromName = data.get("fromName");
        int commentId = Integer.parseInt(data.get("commentId"));
        String toName = data.get("toName");
        User from_user = this.userService.getUserByName(fromName);
        User to_user = this.userService.getUserByName(toName);
        this.commentService.isInsertedR(commentId,from_user.getUid(),fromName,from_user.getAvatar(),to_user.getUid(),toName,to_user.getAvatar(),data.get("content"));
        return this.commentService.getOwnersAllComments(ownerId);
    }

    @GetMapping("/getReplyMe/{username}")
    public List getCommentReplyPersonInfo(@PathVariable("username")String username){
        List<CommentsInfo> replyPersonInfo = this.commentService.getReplyPersonInfo(username);
        Map<String,Object> map = new HashMap<>();
        List all = new ArrayList();
        for (CommentsInfo commentsInfo : replyPersonInfo) {
            List list = new ArrayList();
            Article article;
            if(commentsInfo.getOwnerId().equals("about")){
                article = new Article();
                article.setTitle("关于我");
            }else{
                String aid = commentsInfo.getOwnerId().substring(5);
                article = contentService.getArticleById(Integer.valueOf(aid));
            }
            list.add(article.getTitle());
            list.add(commentsInfo);
            all.add(list);
        }
      return all;
    }
}
